package com.java.a02UDPDemo;

import java.io.IOException;
import java.net.*;

public class SendMessageDemo {
    public static void main(String[] args) throws IOException {
        //1.创建发送端的socket对象
        //DatagramSocket() 构造数据包套接字并将其绑定到本机地址上的任何可用端口
        //通过端口往外发送数据
        //有参：指定端口进行绑定
        //无参：随机端口进行绑定
        DatagramSocket ds = new DatagramSocket();

        //2.打包数据
        String str = "hello,udp I am coming!";
        byte[] bytes = str.getBytes();
        InetAddress address = InetAddress.getByName("LK-9I2IF8OA");
        int port = 10086;
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);

        //3. 调用DatagramSocket的方法发送数据
        //void send(DatagramPacket p)
        ds.send(dp);

        //4.释放资源
        ds.close();
    }
}
